package com.sugon.controller.module;

import cn.afterturn.easypoi.entity.vo.TemplateExcelConstants;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.DataFilter;
import com.sugon.entity.ModuleSpEntity;
import com.sugon.service.ModuleSpService;
import com.sugon.service.SysConfigService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import com.sugon.utils.RRException;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * 模具备件清单Controller
 *
 * @author sugon
 * @date 2021-12-03 16:21:04
 */
@Controller
@RequestMapping("modulesp")
public class ModuleSpController {
    @Autowired
    private ModuleSpService moduleSpService;
    @Autowired
    private SysConfigService configService;

    /**
     * 查看列表
     */
    @DataFilter(deptAlias = "MSS.WORKSHOP", userAlias = "MSS.CREATE_USER")
    @RequestMapping("/list")
    @RequiresPermissions("modulesp:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<ModuleSpEntity> moduleSpList = moduleSpService.queryList(query);
        int total = moduleSpService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(moduleSpList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("modulesp:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ModuleSpEntity moduleSp = moduleSpService.queryObject(id);

        return R.ok().put("moduleSp", moduleSp);
    }

    /**
     * 保存
     */
    @AutoLog("模具备件清单--保存")
    @RequestMapping("/save")
    @RequiresPermissions("modulesp:save")
    @ResponseBody
    public R save(@RequestBody ModuleSpEntity moduleSp) {
        moduleSpService.save(moduleSp);

        return R.ok();
    }

    /**
     * 修改
     */
    @AutoLog("模具备件清单--修改")
    @RequestMapping("/update")
    @RequiresPermissions("modulesp:update")
    @ResponseBody
    public R update(@RequestBody ModuleSpEntity moduleSp) {
        moduleSpService.update(moduleSp);

        return R.ok();
    }

    /**
     * 删除
     */
    @AutoLog("模具备件清单--删除")
    @RequestMapping("/delete")
    @RequiresPermissions("modulesp:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        moduleSpService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ModuleSpEntity> list = moduleSpService.queryList(params);

        return R.ok().put("list", list);
    }

    @RequestMapping("/export")
    @ResponseBody
    public void export(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response) {
        // 设置导出EXCEL模板名称
        modelMap.put(TemplateExcelConstants.FILE_NAME, "模具型芯列表_" + DateUtil.format(DateUtil.date(), "yyyyMMdd"));
        // 设置导出模板参数
        TemplateExportParams templateExportParams = new TemplateExportParams(configService.getPoiTemplatePath() + "moldSPIOTemplate.xlsx");
        templateExportParams.setColForEach(true);
        modelMap.put(TemplateExcelConstants.PARAMS, templateExportParams);
        // 获取设备维护计划预览模板数据
        Map<String, Object> mapData = MapUtil.newHashMap(1);
        // 获取预防性维护计划数据
        mapData.put("moldSPList", moduleSpService.queryList(null));
        modelMap.put(TemplateExcelConstants.MAP_DATA, mapData);
        PoiBaseView.render(modelMap, request, response, TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
    }

    /**
     * 模具备件导入
     */
    @RequestMapping("/import")
    @ResponseBody
    public R importPM(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        int count = moduleSpService.batchImport(file);
        return R.ok("成功导入" + count + "条数据");
    }

}
